package com.hliushi.learn.utils;

// import com.iflytek.biims.testtools.exception.TestToolExceptions;
// import com.iflytek.bp.common.exception.BpException;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author ljgeng
 */
@Slf4j
public class FileUtil {

    public static void deleteDir(String dir) {
        deleteDir(new File(dir));
    }

    public static void deleteDir(File dir) {
        if (!dir.exists() || !dir.isDirectory()) {
            return;
        }
        for (File f : dir.listFiles()) {
            if (f.isDirectory()) {
                deleteDir(f);
            } else {
                f.delete();
            }
        }
        dir.delete();
    }


    /**
     * file.delete(); 方法不能删除有内容的文件夹，所以需要找出这个文件夹下所有内容，依次删除
     *
     * @param file
     */
    public static void deleteFile(File file) {
        // 判断是否是文件
        if (file.isFile()) {
            // 是文件就直接删除
            file.delete();
        }
        // 判断是否是文件夹
        if (file.isDirectory()) {
            // 列出该文件夹下的所有文件目录
            File[] files = file.listFiles();
            if (files.length > 0) {
                // 递归 删除这个文件夹中的所有文件
                for (File file2 : files) {
                    deleteFile(file2);
                }
            }
            // 删除外层文件
            file.delete();
        }
    }


    /**
     * @param response response
     * @param fileName 文件名称
     * @return
     */
    public static void downloadFile(HttpServletResponse response, String fileName) {
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");

        response.setCharacterEncoding("UTF-8");
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        } catch (UnsupportedEncodingException e2) {
            log.error(e2.getMessage());
        }
        byte[] buff = new byte[1024];
        // BufferedInputStream bis = null;
        // OutputStream os = null;


        try (OutputStream os = response.getOutputStream();
             BufferedInputStream bis = new BufferedInputStream(
                     new FileInputStream(new File("src/main/resources/mat/" + fileName)))) {
            // os = response.getOutputStream();
            // bis = new BufferedInputStream(new FileInputStream(new File("src/main/resources/mat/" + fileName)));

            log.info("");
            int i = bis.read(buff);
            while (i != -1) {
                // 下面这行会导致最后一次可能写入无用数据
                // os.write(buff, 0, buff.length);
                os.write(buff, 0, i);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            log.error("The system can not find the file specified, filename = {}", fileName);
            throw new RuntimeException("file not found");
        } finally {
            // if (bis != null) {
            //     try {
            //         bis.close();
            //     } catch (IOException e) {
            //         log.error(e.getMessage());
            //     }
            // }
            // if (os != null) {
            //     try {
            //         os.close();
            //     } catch (IOException e) {
            //         log.error(e.getMessage());
            //     }
            // }
        }

        // 等一会把这个关闭掉
        // deleteFile(new File("src/main/resources/mat"));

    }

}
